const nvl2 = function (str1, str2, str3) {
  return str1 || (str2 || str3)
}

/**
 * 类似Oracle的decode函数
 * (条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)
 * @returns {any}
 */
const contrastDecode = function () {
  if (arguments.length % 2 === 0) {
    for (let i = 0; i < arguments.length; i++) {
      if (arguments[i * 2 + 1]) {
        if (arguments[i * 2 + 1] === arguments[0]) {
          return arguments[i * 2 + 2]
        }
      }
    }
    return arguments[arguments.length - 1]
  }
}

const notice = function (type, msg) {
}

const modal = function (type, msg, handleOk, handleCancel) {
}

const tree2arr = function (data) {
  // 从树形结构转换为数组
  let arr = []
  if (data instanceof Array) {
    data.map(item => {
      arr = arr.concat(this.tree2arr(item))
    })
  } else if (data instanceof Object) {
    const obj = Object.assign({}, data)
    delete obj.children
    arr.push(obj)
    if (!data.children || (data.children && !data.children.length)) {
      return arr
    } else {
      arr = arr.concat(this.tree2arr(data.children))
    }
  }
  return arr
}

const dateFtt = (fmt, date) => {
  if (!date.getTime()) return null
  // author: meizz
  var o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + '').substr(4 - RegExp.$1.length)
    )
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
      )
    }
  }
  return fmt
}

const utc2beijing = (utcDatetime) => {
  // 转为正常的时间格式 年-月-日 时:分:秒
  const TPos = utcDatetime.indexOf('T')
  const ZPos = utcDatetime.indexOf('Z')
  const yearMonthDay = utcDatetime.substr(0, TPos)
  const hourMinuteSecond = utcDatetime.substr(TPos + 1, ZPos - TPos - 1)
  const newDatetime = yearMonthDay + ' ' + hourMinuteSecond // 2017-03-31 08:02:06
  // 处理成为时间戳
  let timestamp = new Date(Date.parse(newDatetime))
  timestamp = timestamp.getTime()
  timestamp = timestamp / 1000
  // 增加8个小时，北京时间比utc时间多八个时区
  timestamp = timestamp + 8 * 60 * 60
  // 时间戳转为时间
  return new Date(parseInt(timestamp) * 1000).toLocaleString().replace(/年|月/g, '-').replace(/日/g, ' ') // 2017-03-31 16:02:06
}

const findObj = (arr, value, key) => {
  let r = null
  arr.some(item => {
    if (item[key] === value) {
      r = item
      return true
    }
    return false
  })
  return r
}

const getTodayStart = () => {
  return new Date(new Date(new Date().toLocaleDateString()).getTime())
}

const getTodayEnd = () => {
  return new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)
}

const _pickerOptions = {
  shortcuts: [
    {
      text: '今天',
      onClick (picker) {
        const end = getTodayEnd()
        const start = getTodayStart()
        picker.$emit('pick', [start, end])
      }
    }, {
      text: '最近一周',
      onClick (picker) {
        const end = getTodayEnd()
        const start = new Date()
        start.setTime(getTodayStart().getTime() - 3600 * 1000 * 24 * 7)
        picker.$emit('pick', [start, end])
      }
    }, {
      text: '最近一个月',
      onClick (picker) {
        const end = getTodayEnd()
        const start = new Date()
        start.setTime(getTodayStart().getTime() - 3600 * 1000 * 24 * 30)
        picker.$emit('pick', [start, end])
      }
    }, {
      text: '最近三个月',
      onClick (picker) {
        const end = getTodayEnd()
        const start = new Date()
        start.setTime(getTodayStart().getTime() - 3600 * 1000 * 24 * 90)
        picker.$emit('pick', [start, end])
      }
    }]
}

const spanMethod = ({ row, column, rowIndex, columnIndex }) => {
  const span = column.property + '-span'
  if (row[span]) {
    return row[span]
  }
}
const mergeTableRow = (data, merge) => {
  if (!merge || merge.length === 0) {
    return data
  }
  merge.forEach((m) => {
    const mList = {}
    data = data.map((v, index) => {
      const rowVal = v[m]
      if (mList[rowVal] && mList[rowVal].newIndex === index) {
        mList[rowVal].num++
        mList[rowVal].newIndex++
        data[mList[rowVal].index][m + '-span'].rowspan++
        v[m + '-span'] = {
          rowspan: 0,
          colspan: 0
        }
      } else {
        mList[rowVal] = { num: 1, index: index, newIndex: index + 1 }
        v[m + '-span'] = {
          rowspan: 1,
          colspan: 1
        }
      }
      return v
    })
  })
  return data
}

export default function () {
  return {
    data () {
      return {
        pickerOptions: _pickerOptions
      }
    },
    computed: {
      app () {
        return this.$store.state.base.app
      },
      oem () {
        return this.$store.state.base.oem
      }
    },
    methods: {
      findObj,
      dateFtt,
      notice,
      modal,
      tree2arr,
      nvl2,
      contrastDecode,
      utc2beijing,
      getTodayStart,
      getTodayEnd,
      spanMethod,
      mergeTableRow
    }
  }
}
